/***************************************************************************
 *   Copyright (C) 2006 by Archimedes Projekt   *
 *   thelsb@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "globals.h"
#include "telemetryservcon.h"
#include <QtDebug>
namespace TM {



QDataStream & operator>>(QDataStream &s, MDataBlock *dataBlock)
{
  
  s.readRawData((char*)dataBlock,sizeof(*dataBlock));
  //qDebug()<<"operator>>";
  return s;
}

QDataStream & operator<<(QDataStream &s, MDataBlock *dataBlock)
{
  s.writeRawData((char*)dataBlock,sizeof(*dataBlock));
  //qDebug()<<"operator<<";
  return s;
}

QDataStream & operator<<(QDataStream &s, MDataBlock::TYPE blockType)
{
  s.writeRawData((char*)&blockType,sizeof(blockType));
  return s;
}

QDataStream & operator>>(QDataStream &s, MDataBlock::TYPE blockType)
{
  s.readRawData((char*)&blockType,sizeof(blockType));
  return s;
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
///QDataStream & operator>>(QDataStream &s, TM_ServCon_Frame& frame)
///not reading in the sync - this has been done
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
QDataStream & operator>>(QDataStream &s, TM_ServCon_Frame& frame)
{
    for(int i=0; i<SYNC_LENGTH; ++i)
        s >> frame.sync1[i];

    s >> frame.FrameCounter;
    s >> frame.Scet;
    s >> frame.Status_Bits;

    // Pressure sensor values - 2 values per frame because sensors are read with 10 Hz
    for(int i=0; i<PRESS_LENGTH; ++i)
        s >> frame.Tank_Line_Pressure [i];
    for(int i=0; i<PRESS_LENGTH; ++i)
        s >> frame.Feed_Pressure [i];
    for(int i=0; i<PRESS_LENGTH; ++i)
        s >> frame.Hose_Pressure [i];
    for(int i=0; i<PRESS_LENGTH; ++i)
        s >> frame.EV1_Pressure [i];
    for(int i=0; i<PRESS_LENGTH; ++i)
        s >> frame.EV2_Pressure [i];

    // Accelerometers - 10 values per frame --> 50 Hz
    for(int i=0; i<ACCEL_LENGTH; ++i)
        s >> frame.X_Accel [i];
    for(int i=0; i<ACCEL_LENGTH; ++i)
        s >> frame.Y_Accel [i];
    for(int i=0; i<ACCEL_LENGTH; ++i)
        s >> frame.Z_Accel [i];
    
    // Gyros give turn rate around the axis - 10 values per frame --> 50 Hz
    for(int i=0; i<RATES_LENGTH; ++i)
        s >> frame.X_Rate[i];
    for(int i=0; i<RATES_LENGTH; ++i)
        s >> frame.Y_Rate[i];
    for(int i=0; i<RATES_LENGTH; ++i)
        s >> frame.Z_Rate[i];

    // Temperatures
    s >> frame.ServCon_Temp; // PCBs
    s >> frame.PCM_Temp;
    s >> frame.Tank_1_Temp; // Gas Tanks
    s >> frame.Tank_2_Temp;
    s >> frame.Tank_3_Temp;
    s >> frame.ICV_Temp;  // Inflation Control Vlave
    s >> frame.Hose_Temp;  // Balloon Hose
    s >> frame.EV1_Temp;  // Equalization Volumes
    s >> frame.EV2_Temp;
    s >> frame.Bat1_Temp;  // Batteries
    s >> frame.Bat2_Temp;
    s >> frame.Bat3_Temp;

    s >> frame.Bat_Voltage; // Battery Voltage
    s >> frame.Current;  // Drain Current

    for(int i=0; i<SYNC_LENGTH; ++i)
        s >> frame.sync2[i];

    return s;
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
///
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
QDataStream & operator<<(QDataStream &s, TM_ServCon_Frame& frame)
{
    for(int i=0; i<SYNC_LENGTH; ++i)
        s << frame.sync1[i];

    s << frame.FrameCounter;
    s << frame.Scet;
    s << frame.Status_Bits;

    // Pressure sensor values - 2 values per frame because sensors are read with 10 Hz
    for(int i=0; i<PRESS_LENGTH; ++i)
        s << frame.Tank_Line_Pressure [i];
    for(int i=0; i<PRESS_LENGTH; ++i)
        s << frame.Feed_Pressure [i];
    for(int i=0; i<PRESS_LENGTH; ++i)
        s << frame.Hose_Pressure [i];
    for(int i=0; i<PRESS_LENGTH; ++i)
        s << frame.EV1_Pressure [i];
    for(int i=0; i<PRESS_LENGTH; ++i)
        s << frame.EV2_Pressure [i];

    // Accelerometers - 10 values per frame --> 50 Hz
    for(int i=0; i<ACCEL_LENGTH; ++i)
        s << frame.X_Accel [i];
    for(int i=0; i<ACCEL_LENGTH; ++i)
        s << frame.Y_Accel [i];
    for(int i=0; i<ACCEL_LENGTH; ++i)
        s << frame.Z_Accel [i];
    
    // Gyros give turn rate around the axis - 10 values per frame --> 50 Hz
    for(int i=0; i<RATES_LENGTH; ++i)
        s << frame.X_Rate[i];
    for(int i=0; i<RATES_LENGTH; ++i)
        s << frame.Y_Rate[i];
    for(int i=0; i<RATES_LENGTH; ++i)
        s << frame.Z_Rate[i];

    // Temperatures
    s << frame.ServCon_Temp; // PCBs
    s << frame.PCM_Temp;
    s << frame.Tank_1_Temp; // Gas Tanks
    s << frame.Tank_2_Temp;
    s << frame.Tank_3_Temp;
    s << frame.ICV_Temp;  // Inflation Control Vlave
    s << frame.Hose_Temp;  // Balloon Hose
    s << frame.EV1_Temp;  // Equalization Volumes
    s << frame.EV2_Temp;
    s << frame.Bat1_Temp;  // Batteries
    s << frame.Bat2_Temp;
    s << frame.Bat3_Temp;

    s << frame.Bat_Voltage; // Battery Voltage
    s << frame.Current;  // Drain Current

    for(int i=0; i<SYNC_LENGTH; ++i)
        s << frame.sync2[i];

    return s;
}

} //namespace TM

